import { CSS2DObject } from '@amcax/base';
import { ctx2 } from '@app-cad/module';

export class CSS2DMarkObject extends CSS2DObject {
  colorID: number;
  canvas = ctx2.viewer.sceneSetup.container;
  pickingData = ctx2.viewer.sceneSetup.pickingData;

  checkVisibility = () => {
    if (this.screenPosition == null) {
      return false;
    }

    let result = false;

    const mouseX = Math.round(this.screenPosition.x);
    const mouseY = Math.round(this.screenPosition.y);

    for (let x = mouseX - 2; x < mouseX + 1; x++) {
      for (let y = mouseY - 2; y < mouseY + 1; y++) {
        const i =
          Math.round(
            x + (this.canvas.clientHeight - y) * this.canvas.clientWidth,
          ) * 4; // 获取像素点index
        const id =
          (this.pickingData.pixelBuffer[i] << 16) |
          (this.pickingData.pixelBuffer[i + 1] << 8) |
          this.pickingData.pixelBuffer[i + 2];

        if (id === this.colorID) {
          result = true;
          break;
        }
      }
    }

    return result;
  };
}
